<?php

class imageUpload {
var $image = array();
	var $upload_dir = 'tmp/recipe/images';
	
    public function upload($file){
    	require_once "./libraries/upload.php";
		//$dir_dest = 'moc';//duong dan upload
		//$dir_tmp = 'moc/tmp';
		//$dir_pics = 'moc';
	    $handle = new Upload($file);
	    $handle->file_new_name_body = md5(rand().time());
	    if ($handle->uploaded) {
	        $handle->Process( $this->upload_dir );
	        // we check if everything went OK
	        	
	        if ($handle->processed) {
	        	$this->image['file_name'] = $handle->file_dst_name;
	        	$this->image['file_size'] = round(filesize($handle->file_dst_pathname)/256)/4;
	        	$this->image['dir'] = $this->upload_dir;
	        	$this->image['error'] = '';
	            // everything was fine !
	            //save image to db
	            $image = Model::factory('Image')->create();
				$image->name = $handle->file_src_name_body;
				$image->path = $handle->file_dst_name;
				$image->create_at = date('Y-m-d H:i:s');
				$image->update_at = date('Y-m-d H:i:s');
				$image->save();
	        	
				return $image;
	        } else {
	        	$this->image['file_name'] = '';
	        	$this->image['file_size'] = 0;
	        	$this->image['dir'] = $this->upload_dir;
	        	$this->image['error'] = $handle->error;
	        	
	        	return false;
	        }
	        // we copy the file a second time
	        //$handle->Process($dir_tmp);

	        // we delete the temporary files
	        $handle->Clean();
	    } else {
	        $this->image['file_name'] = '';
        	$this->image['file_size'] = 0;
        	$this->image['dir'] = $this->upload_dir;
        	$this->image['error'] = $handle->error;
        	
        	return false;
	    }
    }
 
}